package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.flight.domain.BuyTicket;
import cn.wolfcode.luowowo.flight.domain.DoorTicket;
import cn.wolfcode.luowowo.flight.domain.Flight;
import cn.wolfcode.luowowo.flight.domain.FlightTicket;
import cn.wolfcode.luowowo.flight.query.FlightQueryObject;
import cn.wolfcode.luowowo.flight.service.IBuyTicketService;
import cn.wolfcode.luowowo.flight.service.IDoorTicketService;
import cn.wolfcode.luowowo.flight.service.IFlightService;
import cn.wolfcode.luowowo.flight.service.IFlightTicketService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("flight")
public class FlightController {

    @Reference
    private IFlightService flightService;
    @Reference
    private IFlightTicketService flightTicketService;
    @Reference
    private IBuyTicketService buyTicketService;
    @RequestMapping("")
    public String index(@ModelAttribute("qo") FlightQueryObject qo, Model model){

        //查询国内前10热门城市
        model.addAttribute("hotFlights", flightService.selectByHotTop10(qo));
        System.out.println("...");
        //根据首字母查询城市
        model.addAttribute("initialA",flightService.selectByInitialABCDE());
        model.addAttribute("initialF",flightService.selectByInitialFGHJ());
        model.addAttribute("initialK",flightService.selectByInitialKLMNP());
        model.addAttribute("initialQ",flightService.selectByInitialQRSTW());
        model.addAttribute("initialX",flightService.selectByInitialXYZ());
        return "flight/index";
    }
    /**
     * 飞机订票查询
     *
     */
    @RequestMapping("/search")
    @ResponseBody
    public Object search(FlightQueryObject qo) {
        AjaxResult result = flightService.selectByPortAndTime(qo);
        return result;
    }
    /**
     * 预订机票接口
     */
    @RequestMapping("/save")
    public String save(FlightTicket ticket, @UserParm UserInfo userInfo) {

        if(userInfo == null){
            throw  new LogistException("请登录");
        }
        ticket.setUserinfoId(userInfo.getId());
        FlightTicket ti = flightTicketService.saveOrUpdate(ticket);

        return "/flight";
    }

    /**
     *查询该用户的机票订单
     * @param uid 用户id
     * @param model
     * @return
     */
    @RequestMapping("/order")
    public String order(Long uid,Model model,@UserParm UserInfo userInfo) {
        if(userInfo == null){
            throw  new LogistException("请登录");
        }
        List<FlightTicket> orders = flightTicketService.queryByUserinfoId(userInfo.getId());
         model.addAttribute("orders",orders);
        List<BuyTicket> buyTicket = buyTicketService.queryByUserId(userInfo.getId());
        model.addAttribute("doorTickets",buyTicket);
        return "mine/myorder";
    }
    //回显数据给预订机票页面
    @RequestMapping("/showdian")
    public String showdian(Long tid,Model model,@UserParm UserInfo userInfo) {
        if(userInfo == null){
            throw  new LogistException("请登录");
        }
        Flight flight = flightService.queryById(tid);
        model.addAttribute("flight",flight);
        return "flight/show";
    }

}
